package com.vanysoft.framework.config.database;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StringToArrayTypeHandler extends BaseTypeHandler<String[]> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, StringUtils.join(parameter, ","));
    }

    @Override
    public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String columnValue = rs.getString(columnName);
        if (columnValue != null) {
            return columnValue.split(",");
        } else {
            return null;
        }
    }

    @Override
    public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String columnValue = rs.getString(columnIndex);
        if (columnValue != null) {
            return columnValue.split(",");
        } else {
            return null;
        }
    }

    @Override
    public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String columnValue = cs.getString(columnIndex);
        if (columnValue != null) {
            return columnValue.split(",");
        } else {
            return null;
        }
    }

    @Override
    public String[] getResult(ResultSet rs, String columnName) throws SQLException {
        return renderResult(super.getResult(rs, columnName));
    }

    @Override
    public String[] getResult(ResultSet rs, int columnIndex) throws SQLException {
        return renderResult(super.getResult(rs, columnIndex));
    }

    @Override
    public String[] getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return renderResult(super.getResult(cs, columnIndex));
    }

    private String[] renderResult(String[] result) {
        if (result == null) {
            return new String[0];
        }
        return result;
    }
}